package com.markedquilt.comment.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.markedquilt.comment.coomon.*;
import com.markedquilt.comment.model.CategoryModel;
import com.markedquilt.comment.model.SellerModel;
import com.markedquilt.comment.request.CategoryReq;
import com.markedquilt.comment.request.PageQuery;
import com.markedquilt.comment.request.SellerCreateReq;
import com.markedquilt.comment.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.List;

/**
 * @author ZhangKe
 * @Description
 * @date 2020/4/21 0:43.
 */
@Controller("/admin/category")
@RequestMapping("/admin/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @RequestMapping(value = "/index")
    @AdminPermission
    public ModelAndView index(PageQuery pageQuery){

        /** 自动在sql中设置limit offset，rows */
        PageHelper.startPage(pageQuery.getPage(),pageQuery.getSize());
        List<CategoryModel> categoryModelList = categoryService.selectAll();

        /** 获取到当前在第几页，总共有几页 */
        PageInfo<CategoryModel> categoryModelPageInfo=new PageInfo<>(categoryModelList);

        ModelAndView modelAndView = new ModelAndView("/admin/category/index.html");
        modelAndView.addObject("data",categoryModelPageInfo);
        modelAndView.addObject("CONTROLLER_NAME","category");
        modelAndView.addObject("ACTION_NAME","index");
        return modelAndView;
    }

    @RequestMapping(value = "/createpage")
    @AdminPermission
    public ModelAndView createPage(){
        ModelAndView modelAndView=new ModelAndView("/admin/category/create.html");
        modelAndView.addObject("CONTROLLER_NAME","category");
        modelAndView.addObject("ACTION_NAME","create");
        return modelAndView;
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @AdminPermission
    public String create(@Valid CategoryReq categoryReq, BindingResult bindingResult) throws BussinessException {
        if (bindingResult.hasErrors()){
            throw new BussinessException(EmBussinessError.PARAMETER_VALIDATION_ERROR, CommonUtil.processErrorString(bindingResult));
        }
        CategoryModel categoryModel=new CategoryModel();
        categoryModel.setName(categoryReq.getName());
        categoryModel.setIconUrl(categoryReq.getIconUrl());
        categoryModel.setSort(categoryReq.getSort());
        categoryService.create(categoryModel);

        return "redirect:/admin/category/index";

    }

}
